package com.mspbots.script.event;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mspbots.api.client.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Map;

@Slf4j
@Component
public class ScriptAction {

    private static UserClient suserClient;
    private static CoreClient scoreClient;
    private static TeamsClient steamsClient;
    private static WiseClient swiseClient;
    private static AttendanceClient sattendanceClient;
    @Autowired
    private CoreClient coreClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private TeamsClient teamsClient;
    @Autowired
    private WiseClient wiseClient;
    @Autowired
    private AttendanceClient attendanceClient;
    @Autowired
    private ObjectMapper objectMapper;

    public static JsonNode call(String server, String url) {
        return call(server, url, null);
    }

    public static JsonNode call(String server, String url, Object params) {
        return call(server, url, "get", params);
    }

    public static JsonNode call(String server, String url, String methodType, Object params) {
        JsonNode jsonNode = null;
        log.debug("script api server:::" + server);
        switch (server) {
            case "mspbots-user-manage":
                jsonNode = doCall(suserClient, methodType.toLowerCase(), server, url, params);
                break;
            case "mspbots-teams":
                jsonNode = doCall(steamsClient, methodType.toLowerCase(), server, url, params);
                break;
            case "mspbots-wise":
                jsonNode = doCall(swiseClient, methodType.toLowerCase(), server, url, params);
                break;
            case "mspbots-attendance":
                jsonNode = doCall(sattendanceClient, methodType.toLowerCase(), server, url, params);
                break;
            case "mspbots-core":
            default:
                jsonNode = doCall(scoreClient, methodType.toLowerCase(), server, url, params);
                break;
        }
        return jsonNode;
    }

    private static JsonNode doCall(AbstractClient apiClient, String method, String server, String url, Object params) {
        log.debug("http {}{} {}", server, url, params);
        JsonNode jsonNode = null;
        switch (method) {
            case "post":
                jsonNode = apiClient.post(url, params).block();
                break;
            case "get":
            default:
                jsonNode = apiClient.get(url, (Map<String, Object>) params).block();
                break;
        }

        return jsonNode;
    }

    @PostConstruct
    public void init() {
        this.scoreClient = coreClient;
        this.suserClient = userClient;
        this.steamsClient = teamsClient;
        this.swiseClient = wiseClient;
        this.sattendanceClient = attendanceClient;
    }

}
